/**
 * 日志
 * @author 许明浩 微信: muaiyuese
 */
import { getSearchParams } from "./get-search-params";

class Log {
  constructor(_prefixName = "[系统日志]") {
    this._prefixName = _prefixName;
  }
  /**
   * 是否开启
   */
  _isOpen =
    (process.env.TARO_ENV === "h5" && getSearchParams("log")) ||
    process.env.NODE_ENV === "development";
  /**
   * 日志统一前缀
   */
  _prefixName: string;

  /**
   * 开启/关闭debug
   * @param {boolean}
   * @return
   */
  setDebug(_isOpen: boolean) {
    this._isOpen = _isOpen;
  }

  /**
   * debug
   * @param {}
   * @return
   */
  debug(...args: any) {
    if (this._isOpen) {
      try {
        typeof args[0] === "string" && (args[0] = this._prefixName + args[0]);
        console.debug(args);
      } catch (e) {}
    }
  }

  /**
   * info
   * @param {}
   * @return
   */
  info(...args: any) {
    if (this._isOpen) {
      try {
        typeof args[0] === "string" && (args[0] = this._prefixName + args[0]);
        console.info(args);
      } catch (e) {}
    }
  }

  /**
   * error
   * @param {}
   * @return
   */
  error(...args: any) {
    if (this._isOpen) {
      try {
        typeof args[0] === "string" && (args[0] = this._prefixName + args[0]);
        console.error(args);
      } catch (e) {}
    }
  }

  /**
   * error 加深
   * @param {}
   * @return
   */
  errorDeep(...args: any) {
    try {
      typeof args[0] === "string" && (args[0] = this._prefixName + args[0]);
      console.error(
        `%c ${args[0]}`,
        "background:red; padding: 4px; padding-right: 8px; border-radius: 4px; color: #fff;"
      );
    } catch (e) {}
  }

  /**
   * warn
   * @param {}
   * @return
   */
  warn(...args: any) {
    if (this._isOpen) {
      try {
        typeof args[0] === "string" && (args[0] = this._prefixName + args[0]);
        console.warn(args);
      } catch (e) {}
    }
  }

  /**
   * log
   * @param {}
   * @return
   */
  log(...args: any) {
    if (this._isOpen) {
      try {
        typeof args[0] === "string" && (args[0] = this._prefixName + args[0]);
        console.log(args);
      } catch (e) {}
    }
  }

  /**
   * tip
   * @param {}
   * @return
   */
  tip(...args: any) {
    try {
      typeof args[0] === "string" && (args[0] = this._prefixName + args[0]);
      console.log(args);
    } catch (e) {}
  }

  /**
   * table
   * @param {}
   * @return
   */
  table(...args: any) {
    if (this._isOpen) {
      try {
        typeof args[0] === "string" && (args[0] = this._prefixName + args[0]);
        console.table(args);
      } catch (e) {}
    }
  }
}
export default Log;
export const instance = new Log();
